package com.cpa.admin.mapper;

import com.cpa.admin.base.BaseMapper;
import com.cpa.admin.model.ProductDo;
import com.cpa.admin.vo.ProductVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* Created by Mybatis Generator 2018/11/04
 * @author zhao
 */
@Mapper
public interface ProductMapper extends BaseMapper<ProductDo> {


    /**
     * 查找所有vo
     * @return
     */
    @Select("<script> " +
            "select v.id, " +
            "       v.productName as productName, " +
            "       v.productAmount as productAmount, " +
            "       v.settleTime as settleTime, " +
            "       v.settleNumber as settleNumber, " +
            "       v.settleNumber * v.productAmount as settleTotal, " +
            "       v.remark as remark, " +
            "       v.createTime as createTime, " +
            "       v.createUsername as createUsername , " +
            "       v.status as status " +
            "from (select p.id, " +
            "             p.product_name as productName, " +
            "             p.product_amount as productAmount, " +
            "             p.settle_time as settleTime, " +
            "             (select count(1) " +
            "                from user_order uo where uo.delete_flag = 0 " +
            "                                              and uo.product_id = p.id) as settleNumber, " +
            "             p.settle_total as settleTotal, " +
            "             p.remark as remark, " +
            "             p.create_time as createTime, " +
            "             u.username as createUsername , " +
            "             p.status as status " +
            "      from product p " +
            "             left join user u on p.create_by = u.id " +
            "      where p.delete_flag = 0 " +
            "<if test='productName!=null'> " +
            "   and p.product_name like #{productName} " +
            "</if> " +
            ") v" +
            "</script> ")
    List<ProductVo> findAllVo(@Param("productName") String productName);


    /**
     * 查询所有id和name
     * @return
     */
    @Select("select p.id " +
            ", p.product_name as productName " +
            ", p.settle_time as settleTime " +
            "from product p " +
            "where p.delete_flag = 0 ")
    List<ProductVo> findIdAndName();


}